package org.ry.camera.view;

import java.io.IOException;

import org.ry.camera.R;
import org.ry.camera.activity.BaseActivity;
import org.ry.camera.activity.CameraActivity;
import org.ry.camera.tools.LogTool;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.hardware.Camera;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Button;
/**
 * 
 * @author renyang
 *	
 * Preview extends SurfaceView
 */
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
	private SurfaceHolder mHolder;
	private Camera mCamera;
	private Context mContext;
	private OnStartPreviewListener mStartPreview;
	private CameraActivity mCameraInstance;
	public CameraPreview(Context context, Camera camera) {
		super(context);
		mCamera = camera;
		this.mContext = context;
		// Install a SurfaceHolder.Callback so we get notified when the
		// underlying surface is created and destroyed.
		mHolder = getHolder();
		mHolder.addCallback(this);
		// deprecated setting, but required on Android versions prior to 3.0
		mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
		mCameraInstance = ((CameraActivity)context);
	}

	public void surfaceCreated(SurfaceHolder holder) {
		// The Surface has been created, now tell the camera where to draw the preview.
		
		LogTool.i("surfaceCreated");
		try {
			if(mCamera != null)
				mCamera.setPreviewDisplay(holder);
			//mCameraInstance.openAutoFocus();
		} catch (IOException e) {
			LogTool.i("Error setting camera preview: " + e.getMessage());
		}
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		// empty. Take care of releasing the Camera preview in your activity.
		if(mCameraInstance != null){
			mCameraInstance.destroyCamera();
		}
	}

	public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
		// If your preview can change or rotate, take care of those events here.
		// Make sure to stop the preview before resizing or reformatting it.
		
		LogTool.i("surfaceChanged");
		
		if (mHolder.getSurface() == null){
			// preview surface does not exist
			return;
		}

		// stop preview before making changes
		try {
			if(mCamera != null)
				mCamera.stopPreview();
		} catch (Exception e){
			// ignore: tried to stop a non-existent preview
		}

		// set preview size and make any resize, rotate or
		// reformatting changes here

		// start preview with new settings
		try {
			initCamera();
		} catch (Exception e){
			LogTool.i("Error starting camera preview: " + e.getMessage());
		}
	}

	public static void setCameraDisplayOrientation(Activity activity,
			int cameraId, android.hardware.Camera camera) {
		android.hardware.Camera.CameraInfo info =
				new android.hardware.Camera.CameraInfo();
		android.hardware.Camera.getCameraInfo(cameraId, info);
		int rotation = activity.getWindowManager().getDefaultDisplay()
				.getRotation();
		int degrees = 0;
		switch (rotation) {
		case Surface.ROTATION_0: degrees = 0; break;
		case Surface.ROTATION_90: degrees = 90; break;
		case Surface.ROTATION_180: degrees = 180; break;
		case Surface.ROTATION_270: degrees = 270; break;
		}

		int result;
		if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
			result = (info.orientation + degrees) % 360;
			result = (360 - result) % 360;  // compensate the mirror
		} else {  // back-facing
			result = (info.orientation - degrees + 360) % 360;
		}

		camera.setDisplayOrientation(result);
	}

	public interface OnStartPreviewListener {
		void onStartPreview();
	}
	
	public void setOnStartPreviewListener(OnStartPreviewListener mStartPreview){
		this.mStartPreview = mStartPreview;
	}
	public void setCamear(Camera mCamera){
		this.mCamera = mCamera;
		
		if(BaseActivity.isInitCamear){
			try {
				 initCamera();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				LogTool.i("Error starting camera preview: " + e.getMessage());
			}
		}
	}

	public void initCamera() throws IOException{
		if(mCamera != null){
			setCameraDisplayOrientation((Activity)mContext, 0, mCamera);
			mCamera.setPreviewDisplay(mHolder);
			mCamera.startPreview();
			mStartPreview.onStartPreview();
			LogTool.i("initCamera");
		}
	}
	
}
